<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input
          v-model="queryObj.keyword"
          style="margin-bottom: 10px"
          type="text"
          prefix-icon="el-icon-search"
          size="small"
          placeholder="输入员工姓名全员搜索"
          @input="hChange"
        />
        <!-- 树形组件 -->
        <!-- show-checkbox -->
        <el-tree
          ref="tree"
          node-key="id"
          highlight-current
          check-strictly
          :data="depts"
          :props="defaultProps"
          default-expand-all
          :expand-on-click-node="false"
          @current-change="hCurrentChange"
        />
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary" @click="$router.push('/employee/detail')">添加员工</el-button>
          <el-button size="mini" @click="showExcelDidlog=true">excel导入</el-button>
          <el-button size="mini" @click="hExport">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="employlist">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <img v-if="row.staffPhoto" :src="row.staffPhoto" alt="" class="user-avatar">
              <span v-else class="username">{{ row.username.charAt(0) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" label="姓名" />
          <el-table-column prop="mobile" label="手机号" sortable />
          <el-table-column prop="workNumber" label="工号" sortable />
          <el-table-column prop="formOfEmployment" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>未知</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" label="部门" />
          <el-table-column prop="timeOfEntry" label="入职时间" sortable />
          <el-table-column label="操作" width="140px">
            <template v-slot="{ row }">
              <el-button size="mini" type="text" @click="$router.push(`/employee/detail/${row.id}`)">查看</el-button>
              <el-button size="mini" type="text" @click="hAssignRole(row.id)">角色</el-button>
              <el-popconfirm
                title="你确定要删除吗？"
                @onConfirm="hDel(row.id)"
              >
                <!-- <el-button size="mini" type="text" @click="delEmployee(row.id)">删除</el-button> -->
                <el-button slot="reference" size="mini" type="text">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row style="height: 60px" align="middle" type="flex" justify="end">
          <el-pagination
            :current-page="queryObj.page"
            background
            :total="total"
            :page-size="queryObj.pagesize"
            layout="prev, pager,sizes, next"
            :page-sizes="[5, 6, 7, 8]"
            @current-change="currentChange"
            @size-change="handleSizeChange"
          />
        </el-row>
      </div>
    </div>
    <import-excel :show-excel-didlog.sync="showExcelDidlog" @updataEmploye="loadEmployment" />

    <assign-role ref="refRole" :cur-id="curId" :show-role-dialog.sync="showRoleDialog" />
  </div>
</template>

<script>
import { department } from '@/api/department'
import { getEmployeeList, exportEmployee, delEmployee } from '@/api/employee'
import { tranListToTreeData } from '@/utils'
import FileSaver from 'file-saver'
import importExcel from './components/importExcel.vue'
import AssignRole from './components/assignRole.vue'
export default {
  name: 'Employee',
  components: { importExcel, AssignRole },
  data() {
    return {
      curId: -1,
      showExcelDidlog: false,
      showRoleDialog: false,
      depts: [
        {
          name: '传智教育',
          managerName: '总裁',
          children: [
            { name: '总裁办', managerName: '总裁助理' },
            { name: '行政部', managerName: '部长' },
            { name: '财务部', managerName: '会计' }
          ]
        }
      ],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      queryObj: {
        page: 1,
        pagesize: 5,
        keyword: null,
        departmentId: null
      },
      total: 0, // 员工总数
      employlist: [] // 员工列表
    }
  },
  created() {
    this.department()
  },
  methods: {
    async hExport() {
      const res = await exportEmployee()
      // console.log(res)
      FileSaver.saveAs(res, '员工信息表.xlsx')
    },
    hChange() {
      // console.log(this.queryObj.keyword)
      clearTimeout(this.timerId)
      this.timerId = setTimeout(() => {
        this.queryObj.page = 1 // 从第一页开始找
        this.loadEmployment() // 请求
      }, 50000)
    },
    async department() {
      const res = await department()
      // console.log(res)
      // this.depts = res.data
      const rs = tranListToTreeData(res.data)
      this.depts = rs
      const id = rs[0].id
      this.queryObj.departmentId = id
      this.loadEmployment()
      this.$nextTick(() => {
        // this.$refs.tree.setCheckedKeys([id])
        this.$refs.tree.setCurrentKey(id)
      })
    },
    async loadEmployment() {
      // console.log('查询条件是', this.queryObj)
      const res = await getEmployeeList(this.queryObj)
      // console.log(res)
      this.employlist = res.data.rows
      this.total = res.data.total
    },
    hCurrentChange(data, node) {
      // console.log(data, node)
      this.queryObj.departmentId = data.id
      // console.log(this.queryObj.departmentId)
      this.loadEmployment()
    },
    currentChange(page) {
      this.queryObj.page = page
      this.loadEmployment()
    },
    handleSizeChange(val) {
      this.queryObj.pagesize = val
      this.queryObj.page = 1
      this.loadEmployment()
    },
    // async delEmployee(id) {
    //   console.log(id)
    //   await delEmployee(id)
    //   this.$message.success('删除成功')
    //   this.loadEmployment()
    // }
    hDel(id) { // 删除
      // console.log(id)
      delEmployee(id) // 删除
      this.$message.success('删除成功')
      if (this.employlist.length === 1) {
        this.queryObj.page > 1 && this.queryObj.page--
      }
      this.loadEmployment()
    },
    hAssignRole(id) {
      // console.log('当前要分配角色id是', id)
      this.curId = id // 获取id
      this.showRoleDialog = true // 打开弹层
      this.$nextTick(() => {
        this.$refs.refRole.getRoleDeta()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin: 10px;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04c9be;
      font-size: 12px;
      display: inline-block;
    }
  }
}
</style>

<style>
.el-tree--highlight-current .el-tree-node.is-current > .el-tree-node__content {
  color: #276ffb;
  background-color: #f0f4ff;
}
</style>

<style scoped>
.user-avatar {
  cursor: pointer;
  width: 30px;
  height: 30px;
  /* // border-radius: 10px; */
  border-radius: 50%;
}

</style>
